package com.keycloak.admin.controller;

import com.keycloak.admin.model.params.*;
import com.keycloak.admin.services.UserInfo;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/keycloak")
@Api(value = "", tags = "1. userInfo-Api", description = "API")
public class UserInfoController {

    @Autowired
    UserInfo userInfo;

    private Unirest unirest  = new Unirest();

    @ApiOperation(value = "获取验证码", notes = "", httpMethod = "POST")
    @PostMapping("/getClientToken")
    public ReturnParams getClientToken(@RequestBody GetClientTokenParams request) {
        return userInfo.getClientToken(request.getClient());
    }

    @ApiOperation(value = "获取验证码", notes = "", httpMethod = "POST")
    @PostMapping("/getVCode")
    public ReturnParams getVCode(@RequestBody GetVCodeParams request) {
        return userInfo.getVCode(request);
    }

    @ApiOperation(value = "用户注册", notes = "", httpMethod = "POST")
    @PostMapping("/userRegAndLogin")
    public ReturnParams userRegAndLogin(@RequestBody RegParams request,HttpServletRequest request1) {
        return userInfo.userReg(request,request1.getHeader("clientId"),request1);
    }

    @ApiOperation(value = "删除用户", notes = "", httpMethod = "POST")
    @PostMapping("/deleteUser")
    public ReturnParams deleteUser(@RequestBody DeleteUserParams request,HttpServletRequest request1) {
        return userInfo.deleteUser(request,request1.getHeader("clientId"));
    }

    @ApiOperation(value = "查询用户基本信息", notes = "", httpMethod = "POST")
    @GetMapping("/searchUser")
    public ReturnParams searchUser(HttpServletRequest request1) {
        return userInfo.getUserInfo(request1.getHeader("clientId"),request1.getHeader("Authorization"));
    }

    @ApiOperation(value = "设置用户密码", notes = "", httpMethod = "POST")
    @PostMapping("/createPassword")
    public ReturnParams createPassword(@RequestBody CreateUserPasswordParams request,HttpServletRequest request1) {
        return userInfo.createPassword(request,request1.getHeader("clientId"),request1.getHeader("Authorization"));
    }

    @ApiOperation(value = "用户登录", notes = "", httpMethod = "POST")
    @PostMapping("/userLogin")
    public ReturnParams userLogin(@RequestBody CreateUserPasswordParams request,HttpServletRequest request1) {
        return userInfo.userLogin(request,request1.getHeader("clientId"));
    }


    @ApiOperation(value = "用户登出", notes = "", httpMethod = "POST")
    @GetMapping("/userLogout")
    public ReturnParams userLogout(HttpServletRequest request1) {
        return userInfo.userLogut(request1.getHeader("clientId"),request1.getHeader("Authorization"));
    }

    @ApiOperation(value = "用户token获取权限", notes = "", httpMethod = "POST")
    @PostMapping("/parseToken")
    public ReturnParams parseToken(@RequestBody AccessTokenParams request,HttpServletRequest request1) {
        return userInfo.parseToke(request,request1.getHeader("clientId"));
    }

    @ApiOperation(value = "获取用户keycloak信息",notes = "", httpMethod = "POST")
    @GetMapping("/getKeycloakUserId")
    public ReturnParams getKeycloakUserId(HttpServletRequest request1) {
        return userInfo.getUserInfoForMianServer(request1.getHeader("clientId"),request1.getHeader("Authorization"));
    }


    @ApiOperation(value = "乘客端用户注册", notes = "", httpMethod = "POST")
    @PostMapping("/userRegAndLoginPassenger")
    public ReturnParams userRegAndLoginPassenger(@RequestBody RegParams request,HttpServletRequest request1) {
        return userInfo.userReg(request,request1.getHeader("clientId"),request1);
    }

    @ApiOperation(value = "用户迁移", notes = "", httpMethod = "POST")
    @PostMapping("/userRemove")
    public ReturnParams userRemove() {
        return userInfo.userRemove();
    }

   /* @ApiOperation(value = "测试", notes = "", httpMethod = "POST")
    @PostMapping("/test")
    public com.alibaba.fastjson.JSONObject test(@RequestBody AccessTokensParams accessTokensParams) throws IOException{
        System.out.println(2222222);
        HttpResponse<String> response = null;
        try {
            //mainServerUtils.keycloakGetuserIno(realmName,clientId,clientSecrt,token);
            response = unirest.post("https://keycloak.dev.yuedaowang.com"+"/auth/realms/"+"yuedao-dev"+"/protocol/openid-connect/token/introspect")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .field("client_id", "driver")
                    .field("client_secret",  "b279a201-225c-4349-8e5e-283cece51ee1")
                    .field("token", accessTokensParams.getToken())
                    .asString();
            System.out.println(response);
            if(response != null ){
                System.out.println("返回状态值： "+response.getStatus());
            }
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response.getBody());
            return jsonObject;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
           *//* un.shutdown();*//*
        }
    }
*/

}
